<?php

namespace Apps\AppApi\Method\Question;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\UploadPic;
use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionAnswerComments;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosQKeyword;
use Apps\Common\Models\UserBase;
use Apps\Common\Libs\Jpush;


/**
 * 问答回复的评论
 */
class AnswerAddComments extends BaseMethod
{
    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }

        $questionId = intval($this->getDataItem('question_id', 0));
        $answer_id = intval($this->getDataItem('answer_id', 0));
        $content = Keyword::Filter($this->getDataItem('content', ''));
        $user_id = intval($this->getDataItem('user_id', 0));
        $to_user_id = intval($this->getDataItem('to_user_id', 0));

        if ($questionId == 0 || empty($content) || $user_id == 0 || $to_user_id == 0 || $user_id == $to_user_id) {
            return $this->errorLog(ResultStatus::URL_PARAM_CANNOT_EMPTY, '缺少参数或参数错误，请检查！');
        }

        if (mb_strlen($content) > 300) {
            return $this->errorLog(ResultStatus::POST_BODY_FORMAT_ERROR, '回复内容长度不能超过300字符');
        }

        // 身份检查
        if ($Question = Question::findFirst('question_id = ' . $questionId)) {
            $questionData = $Question->toArray();
        } else {
            $questionData = [];
        }

        $UserBase = new UserBase();
        if (!($questionData['user_id'] == $user_id || $UserBase->is_expert($user_id))) {
            return $this->errorLog(ResultStatus::QUERY_DATA_NOT_EXIST, '您没有权限评论！');
        }

        //关键字替换
        $keyword = CosQKeyword::query() -> execute() -> toArray();
        foreach ($keyword as $k => $v){
            $content = str_replace($v['keyword'], '***', $content);
        }

        if (!$answer_id) { // 获取回复的id
            $QuestionAnswer = \Apps\Common\Models\QuestionAnswer::query()->where("question_id = {$questionId} and user_id = {$user_id}")->execute()->getFirst();
            $answer_id = $QuestionAnswer->answer_id ?: 0;
        }

        $up = new UploadPic();
        $data = [
            'question_id' => $questionId,
            'answer_id' => $answer_id,
            'content' => base64_encode(serialize($content)),
            'pics' => json_encode($up->questionPic()),
            'user_id' => $user_id,
            'to_user_id' => $to_user_id,
            'add_time' => date('Y-m-d H:i:s')
        ];

        $QuestionAnswerComments = new QuestionAnswerComments();
        if ($QuestionAnswerComments->save($data)) {
            $dateTime = date('Y-m-d H:i:s');
            (new Question())->getReadConnection()->query("UPDATE question SET last_reply_time = '{$dateTime}' WHERE question_id = {$questionId}");
            $this->setResult(['success' => 1, 'message' => '评论成功！']);
        } else {
            return $this->errorLog(ResultStatus::DATABASE_ERROR, '服务器异常！');
        }
    }

}